<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="filters.movieName" placeholder="名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <org-unit-select v-model="filters.orgUnitId" placeholder="请选择店铺" :org-unit-type="1" />
      <room-select v-model="filters.roomId" placeholder="请选择房间" :org-unit-id="filters.orgUnitId"/>
      <user-select v-model="filters.userId" placeholder="请选择主持人" role="DM" :org-unit-id="filters.orgUnitId" />
      <el-date-picker
        v-model="selectedDate"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        value-format="yyyy-MM-dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions">
      </el-date-picker>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <div style="margin-left: 3.125rem;">
        <el-checkbox v-for="col in dynamicColumn.playRecords" v-model="col.show" class="filter-item" style="margin-left:10px;" @change="tableKey=tableKey+1">
          {{ col.name }}
        </el-checkbox>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="selsChange"
    >
      <el-table-column type="index" width="60" />
      <el-table-column v-if="checkColumn('movieName')" prop="movieName" label="剧本名称" min-width="120" />
      <el-table-column v-if="checkColumn('movieVersion')" prop="movieVersion" label="版本" width="100" />
      <el-table-column v-if="showDmAccount" prop="dmAccount" label="主持人" width="120" />
      <el-table-column prop="dmNickname" label="主持人昵称" width="120" />
      <el-table-column v-if="checkColumn('orgUnitName')" prop="orgUnitName" label="店铺" min-width="120" />
      <el-table-column v-if="checkColumn('roomName')" prop="roomName" label="房间" width="120" />
      <el-table-column v-if="checkColumn('status')" prop="status" label="状态" width="100" :formatter="formatStatus" />
      <el-table-column v-if="checkColumn('startTime')" prop="startTime" label="开始时间" width="160" />
      <el-table-column v-if="checkColumn('endTime')" prop="endTime" label="结束时间" width="160" />
      <el-table-column v-if="checkColumn('terminator')" prop="terminator" label="结束者" width="120" />
      <el-table-column v-if="checkColumn('cost')" prop="cost" label="金额(元)" width="100" :formatter="formatCost" />
      <el-table-column v-if="checkColumn('cost')" prop="luckyAmount" label="优惠扣减(元)" width="120" :formatter="formatLuckyAmount" />
      <el-table-column v-if="checkColumn('cost')" prop="payAmount" label="实付(元)" width="100" :formatter="formatPayAmount" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filters.page" :limit.sync="filters.limit" @pagination="loadPageList" />
  </div>
</template>

<script>
import { listPlayRecords } from '@/api/play-records'
import OrgUnitSelect from '../components/org-unit-select'
import RoomSelect from '../components/room-select'
import UserSelect from '../components/user-select'
import waves from '@/directive/waves' // waves directive
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import util from '@/utils/util.js'

export default {
  name: 'PlayRecords',
  components: { Pagination, OrgUnitSelect, RoomSelect, UserSelect },
  directives: { waves },
  data() {
    return {
      tableHeight: 260,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      sels: [], // 列表选中列
      filters: {
        page: 1,
        limit: 20,
        movieName: '',
        orgUnitId: null,
        roomId: null,
        userId: null
      },
      selectedDate: '',
      orderBy: {
        orderColumn: '',
        order: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近30天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近90天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'formLabelWidth', 'dynamicColumn']),
    showDmAccount() {
      const roles = this.userInfo.roles || []
      return roles.indexOf('Admin') !== -1 || roles.indexOf('Manager') !== -1
    }
  },
  created() {
    this.tableHeight = window.innerHeight - 220
    this.loadPageList()
  },
  methods: {
    checkColumn(col) {
      if (this.dynamicColumn.playRecords.hasOwnProperty(col)) {
        return this.dynamicColumn.playRecords[col].show
      }
      return true
    },
    formatStatus(row) {
      if (row.status === 1) {
        return '播放中'
      } else if (row.status === 2) {
        return '已结束'
      }
      return ''
    },
    formatCost(row) {
      return util.currency.format(row.cost)
    },
    formatLuckyAmount(row) {
      return util.currency.format(row.luckyAmount)
    },
    formatPayAmount(row) {
      return util.currency.format(row.payAmount)
    },
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters, ...this.orderBy }
      if (this.selectedDate && this.selectedDate.length >= 2) {
        param.start = this.selectedDate[0]
        param.end = this.selectedDate[1]
      }
      listPlayRecords(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          const { records, total } = data || {}
          this.list = records
          this.total = total
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.filters.page = 1
      this.loadPageList()
    },
    sortChange({ prop, order }) {
      this.orderBy.orderColumn = prop
      this.orderBy.order = order
      this.handleFilter()
    },
    selsChange(sels) {
      this.sels = sels
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
